package com.growthsee.rider.controller;

import com.growthsee.common.core.domain.AjaxResult;
import com.growthsee.rider.domain.view.RiderAttachmentView;
import com.growthsee.rider.domain.view.WangEditorData;
import com.growthsee.rider.domain.view.WangEditorResult;
import com.growthsee.rider.service.FileUploadService;
import com.growthsee.rider.utils.BasicPathUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Objects;

@RestController
@RequestMapping("file")
@Api("文件管理")
public class FileUploadController {

    @Autowired
    FileUploadService fileUploadService;
    @PostMapping("/uploadFile/{businessId}")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file){
        return  AjaxResult.success(fileUploadService.upload(file));
    }
    @PostMapping("/wangEditor/uploadImage")
    public WangEditorResult wangEditorUploadImage(@RequestParam("file") MultipartFile file){
        RiderAttachmentView upload = fileUploadService.upload(file);
        if(Objects.isNull(upload)){
            return  WangEditorResult.builder().errno(500).build();
        }
        if(!Objects.isNull(upload)){
            return  WangEditorResult.builder().errno(0).data(Arrays.asList(WangEditorData.builder()
                    .url(BasicPathUtil.getCloudurl() +"/"+ upload.getKeyValue())
                    .alt("")
                    .href("").build()
            )).build();
        }
        return null;
    }
}
